/*
 * @(#)ContentsService.java $version 2012. 5. 24.
 *
 */

package com.mywep.myalimi.service;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mywep.myalimi.common.DbConditionHolder;
import com.mywep.myalimi.constant.Constants;
import com.mywep.myalimi.model.NewContent;
import com.mywep.myalimi.model.User;
import com.mywep.myalimi.model.UserContent;
import com.mywep.myalimi.model.url.ContentsUrlParam;
import com.mywep.myalimi.repository.content.NewContentRepository;
import com.mywep.myalimi.repository.content.UserContentRepository;

import com.nhncorp.lucy.spring.core.util.ExtendedMap;

/**
 * @author 한지웅
 */
@Service
public class ContentsService {
	@Autowired
	private UserContentRepository userContentRepository;
	@Autowired
	private NewContentRepository newContentRepository;
	
	
	public Map<String, Object> getNewContents(User user, ContentsUrlParam param) {
		Map<String, Object> datas = new HashMap<String, Object>();

		ExtendedMap userContentKey = DbConditionHolder.getInstance();
		userContentKey.put("id", user.getId());
		userContentKey.put("keyword", param.getKeyword());
		List<UserContent> userContents = userContentRepository.selectByExample(userContentKey);

		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR, -10);
		ExtendedMap newContentKey = DbConditionHolder.getInstance();
		newContentKey.put("keyword", param.getKeyword());
		newContentKey.put("lessThanInsertDate", calendar.getTime());
		List<NewContent> newContents = newContentRepository.selectByExample(newContentKey);
		
		for (NewContent newContent : newContents) {
			for (UserContent userContent : userContents) {
				if (StringUtils.equals(userContent.getUrl(), newContent.getUrl())) {
					newContent.setAddYn(Constants.YesNo.YES.getAcronym());
					break;
				}
			}
		}

		datas.put("newContents", newContents);
		datas.put("keyword", param.getKeyword());

		return datas;
	}
	
	public Map<String, Object> getUserContents(User user, ContentsUrlParam param) {
		Map<String, Object> datas = new HashMap<String, Object>();

		ExtendedMap userContentKey = DbConditionHolder.getInstance();
		userContentKey.put("id", user.getId());
		userContentKey.put("keyword", param.getKeyword());
		userContentKey.put("orderByClause", "insert_date desc");

		List<UserContent> userContents = userContentRepository.selectByExample(userContentKey);

		datas.put("contents", userContents);
		datas.put("keyword", param.getKeyword());

		return datas;
	}

	public void txSaveUserContent(User user, ContentsUrlParam param) {
		NewContent newContentKey = new NewContent();
		newContentKey.setContentKey(param.getContentKey());

		NewContent newContent = newContentRepository.selectByPrimaryKey(newContentKey);
		
		userContentRepository.insert(injectUserContent(newContent, user.getId()));
	}

	/**
	 * 
	 * @param newContent
	 * @param id
	 * @return
	 */
	private UserContent injectUserContent(NewContent newContent, String id) {
		UserContent userContent = new UserContent();
		userContent.setKeyword(newContent.getKeyword());
		userContent.setId(id);
		userContent.setInsertDate(newContent.getInsertDate());
		userContent.setRegDate(newContent.getRegDate());
		userContent.setSummary(newContent.getSummary());
		userContent.setTitle(newContent.getTitle());
		userContent.setUrl(newContent.getUrl());
		userContent.setContextType(newContent.getContextType());
		userContent.setAuth(newContent.getAuth());

		return userContent;

	}

	public void txDeleteUserContent(ContentsUrlParam param) {
		UserContent userContent = new UserContent();
		userContent.setUserContentKey(param.getContentKey());

		userContentRepository.delete(userContent);
	}

	public int newContentsCount(User user, String keyword) {
		ExtendedMap userContentKey = DbConditionHolder.getInstance();
		userContentKey.put("id", user.getId());
		userContentKey.put("keyword", keyword);
		List<UserContent> userContents = userContentRepository.selectByExample(userContentKey);

		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR, -10);
		ExtendedMap newContentKey = DbConditionHolder.getInstance();
		newContentKey.put("keyword", keyword);
		newContentKey.put("lessThanInsertDate", calendar.getTime());
		List<NewContent> newContents = newContentRepository.selectByExample(newContentKey);

		int count = 0;
		for (NewContent newContent : newContents) {
			for (UserContent userContent : userContents) {
				if (StringUtils.equals(userContent.getUrl(), newContent.getUrl())) {
					count++;
					break;
				}
			}
		}

		return newContents.size() - count;

	}

	public int userContentsCount(User user, String keyword) {
		ExtendedMap param = DbConditionHolder.getInstance();
		param.put("id", user.getId());
		param.put("keyword", keyword);
		return userContentRepository.countByExample(param);

	}


}
